#include <iostream>
#include <stack>
using namespace std;
//更新版，只用2个栈就可以实现
//一个栈记录货物信息，一个辅助栈记录极大值
stack<int> goods, maxgood;
int n;
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int t;
        cin >> t;
        if (t == 0) {
            cin >> t;
            goods.push();
            if (maxgood.empty()) {
                maxgood.push(t);
            } else {
                maxgood.push(max(t, maxgood.top()));
            }
        } else if (t == 1) {
            if (!goods.empty()) {
                goods.pop();
                maxgood.pop();
            }
        } else if (t == 2) {
            if (goods.empty()) {
                cout << 0 << endl;
            } else {
                cout << maxgood.top() << endl;
            }
        }
    }
    return 0;
}
